/**
 * 
 */
package com.orcwar.engine.randomizer;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.WordUtils;

import com.orcwar.util.RandomGenerator;

/**
 * @author Callil
 *
 */
public class OWOrcNameRandomizer {
	public static final int MAX_SYLLABLES = 3;

	private static OWOrcNameRandomizer instance;
	
	private static List<String> syllables;
	
	
	public static synchronized void initiliaze(InputStream is) {
		syllables = new ArrayList<String>();
		java.util.Scanner s = new java.util.Scanner(is).useDelimiter(",");
		while (s.hasNext()) {
			syllables.add(s.next());
		}
	}
	
	public static OWOrcNameRandomizer getInstance() {
		if (instance == null) {
			throw new IllegalStateException("FUCK YOU ! Initialize first");
		}
		return instance;
	}
	
	/**
	 * 
	 */
	private OWOrcNameRandomizer() {		
	}
	
	
	/**
	 * Generate an orc name.
	 * @return the name generated
	 */
	public static String generateName() {
		StringBuilder sb = new StringBuilder();
		
		int nbSyllables = RandomGenerator.randomIntInRange(1, MAX_SYLLABLES);
		for (int i=0 ; i<nbSyllables ; i++) {
			int index = RandomGenerator.randomIntInRange(0, syllables.size() - 1);
			sb.append(syllables.get(index).trim());
		}
		
		return WordUtils.capitalize(sb.toString().trim());
	}
	

}
